//
//  XJWalletFinanceCell.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/23.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  资金明细cell

#import "XJWalletFinanceCell.h"
#import "XJWalletFinanceModel.h"

@interface XJWalletFinanceCell()

@property(nonatomic, strong) UIView *bgView; // <div> </div>
@property(nonatomic, strong)UILabel *moneyLable; // -30.00 , +100.0
@property(nonatomic, strong)UILabel *nameLabel;
@property(nonatomic, strong)UILabel *timeLabel;


@end
@implementation XJWalletFinanceCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - 一
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self.contentView addSubview:self.bgView];
    
    [self.bgView addSubview:self.moneyLable];
    [self.bgView addSubview:self.nameLabel];
    [self.bgView addSubview:self.timeLabel];
}

#pragma mark - 二 lazy
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}

- (UILabel *)moneyLable {
    if (!_moneyLable) {
        _moneyLable = [UILabel new];
        _moneyLable.font = XJFONTSET_M(18.0);
    }
    return _moneyLable;
}


- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
    }
    return _nameLabel;
}


- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.textColor = XJCOLOR_RGB(110, 110, 110);
        _timeLabel.font = XJFONTSET_R(14.0);
        
    }
    return _timeLabel;
}


#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    // self 60
    
    CGFloat k15 = 15;
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    [self.moneyLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView.mas_left).offset(k15);
        make.centerY.equalTo(self.bgView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(100, 40));
    }];
    
    // top:10 h:20
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.moneyLable.mas_right);
        make.top.equalTo(self.bgView.mas_top).offset(10);
        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.moneyLable.mas_right);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];;
}



#pragma mark - 赋值
- (void)setFinanceModel:(XJWalletFinanceModel *)financeModel {
    _financeModel = financeModel;
    
    NSString *plusMinus = nil;
    
    if ([financeModel.finance_sign isEqualToString:@"1"]) {
        plusMinus = @"+";
        self.moneyLable.textColor = [UIColor redColor];
    }else {
        plusMinus = @"-";
        self.moneyLable.textColor = XJCOLOR_RGB(1, 126, 0);
    }
    
    self.moneyLable.text = [NSString stringWithFormat:@"%@%.2f",plusMinus,[financeModel.money floatValue]];
    
    if ([financeModel.comment isEqualToString:@"订单号支付"]) {
        self.nameLabel.text = @"支付订单";
    }else {
        self.nameLabel.text = financeModel.comment;
    }
    
    self.timeLabel.text = [self timestampSwitchTime:[financeModel.create_time integerValue]];
}

#pragma mark - Private methods
/// 时间戳转时间字符串
- (NSString *)timestampSwitchTime:(NSInteger)timestamp{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"]; // （@"YYYY-MM-dd hh:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:(timestamp / 1000.0)];
    
//    NSLog(@"1296035591  = %@",confromTimesp);
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    //NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);
    
    return confromTimespStr;
}

@end
